<?php

namespace app\admin\model;

use think\Db;
use think\Model;

class KuaiDiHelpOrderModel extends Model {

    public function getHelpOrderList($where, $start_time, $end_time, $page, $size)
    {

        $offset = ($page - 1) * $size;
        if ($offset <= 0) {
            $offset = 0;
        }
        
        if(!empty($start_time) && !empty($end_time)) {
            $where['create_time'] = ['between time', [$start_time, $end_time]];
        }
        $list = Db::table('kuaidi_help_order')->where($where)->limit($offset, $size)->order('id desc')->select();
        $total = Db::table('kuaidi_help_order')->where($where)->count();
        $optionIds = [];
        foreach($list  as $val) {
            $optionIds[] = $val['option_id'];
        }
        $optionsData = Db::table('kuaidi_options')->where('id', "in", $optionIds)->select();
        foreach ($optionsData as $item) {
            $optionsList[$item['id']] = $item['name'];
        }
        foreach($list  as &$vall) {
            $vall['option_name'] = isset($optionsList[$vall['option_id']]) ? $optionsList[$vall['option_id']] : "其他";
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $total_page = 0;
        if ($total) {
            $total_page = ceil($total / $size);
        }
        $res['data']['list'] = $list;
        $res['data']['total'] = $total;
        $res['data']['total_page'] = $total_page;
        return $res;

    }

    public function getHelpOrderListData($where, $start_time, $end_time)
    {


        if(!empty($start_time) && !empty($end_time)) {
            $where['create_time'] = ['between time', [$start_time, $end_time]];
        }
        $list = Db::table('kuaidi_help_order')->where($where)->order('id desc')->select();
        if(empty($list)) {
            return [];
        }
        $optionIds = [];
        foreach($list  as $val) {
            $optionIds[] = $val['option_id'];
        }
        $optionsData = Db::table('kuaidi_options')->where('id', "in", $optionIds)->select();
        foreach ($optionsData as $item) {
            $optionsList[$item['id']] = $item['name'];
        }
        foreach($list  as &$vall) {
            $vall['option_name'] = isset($optionsList[$vall['option_id']]) ? $optionsList[$vall['option_id']] : "其他";
        }

        return $list;

    }

    public function upHelpOrderStatus($where, $updata) {
        $ret = Db::table('kuaidi_help_order')->where($where)->update($updata);
        
        $res['success'] = true;
        $res['desc'] = '';
        $res['data'] = [];
        $res['errorcode'] = '';
        return $res;
        
    }

}